#include <bits/stdc++.h>
using namespace std;

// class Solution {
// public:
//     int search(vector<int>& nums, int target) {
//         int low = 0, high = nums.size(), mid;
//         while(low < high){
//             mid = (low + high) / 2;
//             if(nums[mid] == target)return mid;
//             else if(nums[mid] < target)low = mid + 1;
//             else high = mid;
//         }
//         return -1;
//     }
// };

/**
 * 2021.9.6
 * 注意二分的写法细节
 * 一种使用左闭右闭区间 [left, right]
 * 一种使用左闭右开区间 [left, right)
 * 两种写法初始化边界值、while 语句判断条件、high的更新写法都有不同。
 */


class Solution {
public:
    int search(vector<int>& nums, int target) {
        int low = 0, high = nums.size();
        while(low < high){
            int mid = (low + high) / 2;
            if(nums[mid] < target)low = mid + 1;
            else if(nums[mid] > target)high = mid;
            else return mid;
        }
        return -1;
    }
};
/**
 * @brief 2022.3.1重写
 * 
 */